<?php
include_once '../inc/global.php';

$mid = $_GET['mid'] ? $_GET['mid'] : 3;

//二级菜单ID
$sql = "SELECT mid,name FROM menu WHERE pid = {$mid} ORDER BY sort ASC";
$ret = $db->getAll( $sql );
$arr_2 = $arr_2_id = array();
foreach( $ret as $v ){
	$arr_2[] = $v;
	$arr_2_id[] = $v['mid'];
}


//在一级菜单ID下的所有的二级菜单模块ID
$str = empty( $arr_2_id ) ? 0 : implode(',',$arr_2_id);
$sql = "SELECT MID,MUrl,Menu_id,Menu_name FROM Mould WHERE Menu_id IN({$str}) ORDER BY MID ASC";
$ret = $db->getAll( $sql );
$menu_arr = array();
foreach( $ret as $v ){
	$menu_arr[$v['Menu_id']][] = $v;
}

//用户模块权限
$power = $_SESSION['CPower'] ? unserialize($_SESSION['CPower']) : array();
$power_arr = array();
foreach( $power as $v ){
	$power_arr[] = $v['MID'];
}

foreach( $arr_2 as $k=>$v ){
	$str_1 = $str_2 = $str_3 = '';
	$str_1 .= '<h3 class="f12"><span class="switchs'.($k <= 1 ? ' on' : '').'" title="展开与收缩"></span>'.$v['name'].'</h3>';
	$str_1 .= '<ul '.($k > 1 ? 'style="display:none;"' : '').'>';
	if( !empty($menu_arr[$v['mid']]) ){
		foreach( $menu_arr[$v['mid']] as $v2 ){
			if( $_SESSION['AID'] != 1 && !in_array( $v2['MID'], $power_arr ) ) continue;//模块权限控制
			$str_2 .= '<li id="_MP'.$v2['MID'].'" class="sub_menu"><a href="javascript:_MP('.$mid.','.$v2['MID'].',\''.$v2['MUrl'].'\');" hidefocus="true" style="outline:none;">'.$v2['Menu_name'].'</a></li>';
		}
	}
	$str_3 .= '</ul>';
	
	echo $str_2 ? $str_1 . $str_2 . $str_3 : '';
}
	
echo '<script type="text/javascript">
		$(".switchs").each(function(i){
			var ul = $(this).parent().next();
			$(this).parent("h3").css("cursor","pointer");
			$(this).parent("h3").click(function(){
				if(ul.is(":visible")){
					ul.hide();
					$(this).children("span").removeClass("on");
				}else{
					ul.show();
					$(this).children("span").addClass("on");
				}
			})
		});
	</script>';

